Can’t see why it would against the law. I do it regularly. Usually to take in a view I cannot see whilst seated.
I remember reading one of the White Dalton blogs about it: https://www.whitedalton.co.uk/motorbike-blog/2017/11/dont-stand-for-it/
Not having the bike "under full control" would be the first thing that comes to mind of any plod not familiar with motorcycles which is complete rubbish as anyone who rides off road knows that standing on the pegs gives you more control. Obviously we're talking about standing on the pegs and keeping your hands on the bars here! On the road bikes, if I hit a particularly rough patch of road or maybe a cattle grid I regularly stand on the pegs or at least lift my backside off the seat several inches which not only helps the bike ride the surface but also aids comfort. When out on the rough stuff with the dirt bike I reckon I'm up on the pegs 80-90% of the time. On bikes with high bars and a more upright riding position it just feels natural, on sports bikes with an aggressive racing crouch not so much.
It's legal, think of all those GS riders going to starbucks getting in trouble if it wasn't Even the pillion can sit or stand whatever way they want, even backwards! The only requirement (for the pillion) is their feet can reach the pegs.
If a cop says its illegal the judge will agree even if its not.....so if I do it.....I would avoid doing it in front of a cop. When I do it.....not very often....it worries me the bolt holding the peg on breaking.....that would spoil yout day for sure.