1640s, "play on words, pun," shortened from whimwham "fanciful object" (q.v.). Meaning "caprice, fancy, sudden turn or inclination of the mind" first recorded 1690s, probably a shortened form of whimsy.
Entries linking to whim
whimwham n.
"whimsical device, trifle," 1520s, of unknown origin; perhaps from Scandinavian (compare Old Norse hvima "to let the eyes wander," Norwegian kvima "to flutter"), or else an arbitrary native formation (compare flim-flam).