I've seen movies and tv shows that feature mermaids shapeshifting to grow legs (Pirates of the Caribbean) and I'm wondering if this has any ancient myths behind them?
This theme seems to have become popular in mainstream culture from Hans Christian Andersen's The Little Mermaid. A 19th century fairy story.
However, the idea of a water creature like a mermaid giving up her tail to live on land goes back to at least the 14th century.
The folk story of Melusine, a water nymph living in a holy well or river, can be dated to this time. Melusine would change her tail for legs to live on land with her love Raymond, except for Saturdays, when she went back to the water. This appears in a collection of tales compiled by Jean d'Arras about 1390.
There is a version of the story linking Melusine to both Scotland and to the Isle of Avalon. Thus linking her to Arthurian legend.
There have been many retellings and reworkings of that story, keeping the basic theme of a mermaid seeking to live on land for the love of a man. I've never heard of a version with a male water creature.