Kill the power to the house. Open up the HWT access panel outside and make sure each electrical connection is clean and tight. You might have corrosion and/or looseness causing an intermittent connection. Anything suspicious, clean the terminal spades with steel wool and soak the wire-ends in CLR, then rinse them in a small cup of water, dry off, crimp-tighter if they slip over too easily, and hope for the best.
If your problem is deeper yet, then my advise ends there.