The story goes back more than 1000 years to the Arab empire of the 600-700s. When the empire expanded up through Northern Africa into what's now Sicily, the Arabs brought their foods with them. One of the foods they brought with them was a sweet "cookie" made from honey and a ground up nut flour - probably pistachio, but possibly also almond or other nuts.
The Sicilians called this food "maccheroni" as it was made up of a ground nut and maccheroni referred to any food, savory or sweet, made from something ground up - nut or wheat.
In the 1500s, Catherine de Medici married into French royalty and moved to France. It was here that she introduced the sweet Italian "maccheroni" to the French. Upon their adoption into French cuisine, the Italian maccheroni were directly translated into French as "macarons."
As coconut grew in popularity from its introduction to Europe by Vasco de Gama in the 1500s to the international food boom in post WWI and WWII United States, chefs and bakers sought out new ways to use new ingredients. The swap of almond flour for shredded coconut was, as far as we're concerned, a stroke of brilliance that stuck with the market.