The most likely explanations would be either 1) a loose connection, or 2) if the moped has a self-resetting circuit breaker (instead of a fuse) in the headlight circuit, then perhaps there is an overload.
The first possibility would most likely be set off by vibrations, so you might think about when the light goes out and see if you notice a pattern – does it happen when going over bumps or on a rough patch of road? Or perhaps when you brake? The loose connection could be in the headlight itself as well as the wiring to it or the connections within the fixture.
The suggestion to bypass the earth (ground) wire may or may not resolve the problem, but it would help to eliminate the ground as the source of the problem. Similarly you could bypass the power supply to the light. For starters you could make the light circuit as simple as possible (but I'd recommend including a fuse if you bypass all the way back to the battery).
If you bypass both the power and the ground back to the battery with just a fuse and no switch then the light should work reliably. If it doesn't then the problem lies either with the light, your wiring or the battery. If it does work then either you've solved the problem or, at the very least you have a reliable light while you work on sorting out where the problem actually lies.
The second possibility seems much less likely, both because fuses are more common and because of the different lengths of time before the light comes back on. But if your moped has a circuit breaker this would be worth pursuing.