My friends daughter did this. She was later diagnosed with OCD, but what helped at the time was popping her in a pull up.
My friend reassured her that she didn't need to go to the toilet all the time or worry about having an accident because if the worst happened, the pull up would catch it.
She encouraged her to drink loads and when her daughter saw that she wasn't having accidents (the pull up was dry) and that she was able to drink plenty, she got into a much better mindset.
It's important it's not seen as a punishment, more a medical device. Also being told it's all in her head won't help.
I also have OCD and sometimes when anxious feel like any little tickle means I need to go to the toilet, even if it's a few drops. This isn't good as it is bad for bladder control, so I don't allow myself.
But all this is to say it is a relatively common behaviour for some people. We just need to help children stop because they don't understand the wider impact.
(This advice is assuming there really is no medical issue).