While he was in the bath, a good idea suddenly popped into his head.

Literal

Bath [target-に] entering [while-ている-時], good idea [subject-が] suddenly his head [target-に] floated.

彼の頭に浮かんだ ('floated into his head') makes the location of the inspiration explicit — 浮かぶ ('to float up') is the standard metaphor for ideas surfacing in consciousness. 突然 adds the suddenness of the insight.