Is there a story or meaning, or is it just random and whimsical fashion?
This hat has no meaning, story or anything like that in Germany. It's just as you said... a random fashion stunt. There are quite a lot of hats and caps in these colors. People just like them.
During the World Cup or other major events the designers try to sell merchandising and the look of the country's colors can be printed on anything and everyone sells shirts and flags, etc. So you need fresh ideas to sell your stuff and these hats are nothing more than that - a clever merchandising idea.
The only hat people wear during the World Cup with a real German origin is this one:
It's a hat from Bavaria (Seppelhut):