Sometimes it's a mild anxiety sometimes its just a boredom reflex. I would discourage it gently.
My oldest was cured from nail biting overnight buy showing her a live infrared live video of her hands both before and after washing them with soap. The video showed the staggering number of germs ( bacteria) on her hands at any one time. Se never put them in her mouth again! She also won't pat the dogs without washing her hands after now or eat without washing her hands first.