The actual reason for the origin of feathers is fairly unclear. Assuming that pterosaur pycnofibres are homologous (and, due to parsimony, and the unlikelihood that a similar structure would evolve three separate times in three closely related groups when compared to the possibility that it was an ancestral trait, I am fully convinced that they’re homologous [simpler terms: they probably were]), that means that protofeathers were an ancestral trait for all of Ornithodira, and possibly Avemetatarsalia. This would indicate that its an ancient structure, as the group got their origins around the middle Triassic.
Archosaurs in general (the group that includes Avemetatarsalia - dinosaurs and pterosaurs - as well as crocodilians) only evolved in the early Triassic, right after the Permian-Triassic extinction (the largest in our Earth’s history, wiping out 96% of marine life and 70% of terrestrial life). This extinction had a variety of causes, though the ultimate major contributing factors are under debate. It is possible that there was extensive volcanic activity; this could have triggered major climate change that affected the planet in a variety of ways (though many other events happened at the same time, such as the continued formation of Pangaea, and anoxia in the oceans).
It is possible, therefore, that the climate change events due to the Permian extinction may have lead to the need for insulation to evolve in archosaurian ancestors; these structures then could have been secondarily lost in crocodilians and non-avemetatarsalian archosaurs. After all, they were definitely secondarily lost in dinosaur groups such as hadrosaurs. This secondary loss could have been triggered by the increase in global temperature in the Early Triassic (which caused a minor mass extinction, almost undetectable since almost everything was already dead at that point).
So, if protofeathers were no longer needed for insulation (though, keep in mind, that feather-like structures can be used for insulation from both the cold and the heat), why would they have stuck around in Avemetatarsalians?
One word: Display
Look at birds today. Those suckers LOVE to show off for each other in order to reproduce. It is heavily hypothesized that protofeathers were used to display for reproduction between dinosaurs (and potentially pterosaurs), and thus, keeping protofeathers lead to a greater likelihood of reproductive success.
This is just one hypothesis for why protofeathers might have evolved, though, and it is based on extensive conjecture - we have no evidence on whether or not archosaurian ancestors were protofeathered from fossils. Crocodilians and birds (all living archosaurs, in other words) do share homologous protein genes that contribute to the development of avian feathers and scales as well as crocodilian scales. This definitely indicates that the same protein structure lead to the evolution of these integument types in these two different groups. Whether or not, however, crocodilian scales are modified protofeathers (avian scales are reverted feathers) is the main question at hand, and requires more active molecular biology research (I am extremely interested in studying this).
At the very least, the findings that crocodilians and birds share these proteins indicates that it is possible that protofeathers may be an ancestral trait. If there had been no homologous gene in crocodilians, this would have been impossible. Furthermore, given that feathers have a really hard time fossilizing, the probability of finding a feathered archosaur ancestor is fairly low (especially since they weren’t common - I mean, nothing was common, that’s why it’s called a Mass Extinction). The best avenue of continued research in this topic is molecular developmental biology; and a lack of feathered early archosaurs does not mean this hypothesis is unlikely, IMO.
(Sorry, paleontologists and biologists clash sometimes on how to rank different kinds of evidence. For example, it was resolved through a combination of fossil and genetic data that turtles were actually a group of archosauromorphs (not archosaurs, but close;) still, a paleontologist later came out with a paper indicating they were related more closely to lizards instead, based solely on fossil evidence. I find this hard to swallow; analyzing differences in skeletal bones and fossils is a lot more subjective than comparing homologous protein genes. Though it is possible to identify fossils quantitatively, as shown in the Tschopp et al. analysis of sauropods this year, it is extremely difficult - much more difficult than it is to analyze genetic similarities quantitatively. I’m not saying fossil evidence is unreliable or misleading; I’m saying that if genetic evidence is available, it should be utilized along with fossil evidence. In the case of the turtles, the latter analysis did not do so.)
So that’s my two cents. If anyone else has a hypothesis on how protofeathers evolved (and, obviously, when,) please feel free to comment!