Bemidbar 25:1-3 records that the Jewish people sinned with the daughters of Moav, and G-d punished the people by sending a plague. The plague ended after Pinhas killed two of the brazen sinners, 25:6-8. 25:11-13 then records the rewards that Pinhas was to receive for his actions, and this apparently was the end of the incident. However, afterwards, 25:14,15 identifies the brazen sinners as Zimri, the son of Salu, a chieftain in the tribe of Shimon, and Kozbi, the daughter of one of the leaders of Midyan. The fact that the Torah identifies the sinners is not so unusual, but one would have thought that if the Torah was going to identify the sinners, then this would have occurred when they were first referred to in 25:6. Why initially did the Torah describe the sinners anonymously, and only after the episode was apparently over, did the Torah reveal their identity? (For some answers, see Ramban on 25:18 and Or ha-Chayyim on 25:14.)
My guess is that the Torah is trying to present the state of mind of Pinhas at the time when he acted. Just as the reader was unaware of the identity of the sinners until the episode was over, so too Pinhas was unaware of the sinner's identities when he acted. Moshe had ordered that the sinners were to be killed, 25:5, and Pinhas obeyed without inquiring who were the people sinning. This idea explain why the Torah initially presented the sinners anonymously, but then why is important for us to know their identity afterwards? The following verses suggest an answer.
25:16-18 record that G-d commanded the Jewish people to attack the Midyanites due to “the matter of Peor and the matter of Kozbi.” Is the “matter of Kozbi” identical with the events of Baal Peor? If yes, then why is it mentioned twice?
Ibn Ezra (on 25:18) suggests that 25:18 is listing two separate reasons why Midyan was to be attacked. One reason was for leading the people astray by Baal Peor and the second reason was that Midyan wanted to harm the people out of revenge for Kozbi's death, their Midyanite princess, because they blamed the Jewish people for her death. I understand this harm to mean that Midyan attacked the people after the events of Baal Peor.
A proof for this is that 25:18 uses the word, tsorerim, which implies ongoing hostility, see Milgrom, 1990, p.218. Furthermore, the Torah uses the same word, tsoror, assail, harass, when commanding the Jewish people to attack Midyan, 25:17. The double use of the same word implies that the attack was tit for tat, Midyan was to be attacked because they were attacking you. 25:18 could then be understood as “for they (Midyan) are attacking you with their craftiness (guerrilla warfare?), with which they were crafty with you in Baal Peor and in the matter of the death of Kozbi (that they blamed the Jewish people for her death).
In addition, when the command to fight Midyan is repeated in 31:2, the Torah records that the Jewish people are to take revenge on Midyan. Ibn Ezra (on 31:2) explains that the revenge is because Midyan caused the Jewish people to worship idolatry. Yet, Midyan did not force the people to commit idolatry. Instead, if Midyan had been attacking the people and were continuing to attack them, then it makes sense that the Jewish people were to seek revenge to stop these attacks.
This idea explains why the people were commanded to attack Midyan and not Moav who were also involved in the sins of Baal Peor. Rashi (on 25:18) explains that Moav was not to be attacked since Ruth was to come from Moav. Ramban (on 25:18) quotes a Midrash that Moav was not attacked since they had acted due to their fears while Midyan got involved in the sin of Baal Peor for no reason. Ramban also writes that Moav was protected due to their forefather Lot, as Devarim 2:9 records that G-d told Moshe the Jewish people could not fight with Moav. The idea that Midyan was attacking the Jewish people suggests another reason. Only Midyan was attacked since they attacked the Jewish people, while Moav was not attacked since they did not attack the Jewish people.
Finally, this idea also explains why Kozbi had to be identified. Midyan would only want to take revenge if the Jewish people had killed an important person, and from the identification of Kozbi we learn that she was a princess of Midyan. Thus, she is mentioned in 25:15 immediately prior to the command to attack Midyan, and the explanation that the command was due to the "matter of Kozbi." Therefore, had Torah not identified her we would not have understood why the Jewish people fought with Midyan.
We are left with the question why did Zimri have to be identified? Possibly once Kozbi was identified, then Zimri, her partner, was also mentioned.
My daughter, Talia, has suggested that Zimri was identified in order that Midyan should realize that Kozbi was not killed because she was a foreigner, but since she sinned, as a "high ranking" Jew, Zimri, was also killed. Thus, Zimri was identified to show that Midyan had no justification for their attacks on the Jewish people, and again the identification was recorded in conjunction with the command to attack Midyan, which was in response to the attacks from Midyan.
A third reason is that with the identification of Zimri we also learn that he was from one of the leading families of the tribe of Shimon, and then his death accorded with G-d's instruction to Moshe in 25:4, to kill the leaders of the people who were sinning. Moshe had not relayed this information to the people, 25:5, and hence when Pinhas acted he was consciously fulfilling Moshe's instructions and unintentionally G-d's instructions.