some children find comfort. my son does this since he was 1yr. I used to and i did this from 8 and up its a comfort zone. for me its becuz the waist band on top of my hand makes me feel secure. if that makes sense
She may also be holding herself with the need to use the bathroom, I have seen children do this when they want to go and dont like to ask ..Does this happen mostly at school as teachers are often amiss at asking if children want to use the bathroom?
Doctor is right to say not to punish the child for this. It's probably just a comfort habit. I would tell her as gently as possible that it is not considered appropriate to touch one's private parts when around other people, and that she has to limit that kind of thing to her bedroom.