It could be an intermittent short. The headlights are on a circuit breaker instead of a fuse, and will flicker on and off as the breaker resets.
It could also be a loose connection on the power feed side of the headlight switch. Does it happen more when going over bumps, etc.?