Headlights come and go
I think Red is on the right track, but if it's an intermittent connection, fiddling the switch might make it "arc over" and make connection.
I would check ground and connections at the harness up front
Check the condition of the harness connector at the light switch
ditto the harness connector at the dimmer switch
Does this happen on both high and low beam, or only on one? This might indicate a dimmer problem, or bad connection where the harness goes through the bulkhead.
As above, there is a breaker in the switch. These breakers can become weak, or if the switch or switch connector starts to generate heat from bad connections, this can cause breaker cycling.