This is discussed here including R' Chaim Brisker's reaction to this Torah.
וירא פינחס בן אלעזר בן אהרן הכהן, ויקח וכו'. ולא יעלה ח"ו על הדעת לומר שזמרי היה נואף ח"ו כי מן הנואף לא עשה הקב"ה פרשה בתורה, אך יש סוד בדבר זה, דהנה יש יו"ד נקודות בזנות, הנקודה הא' מי שמקשט עצמו והולך במזיד לדבר עבירה היינו שהאדם בעצמו מושך עליו היצה"ר ואח"ז יש עוד ט' מדרגות, ובכל המדרגות שניטל מהאדם כח בחירתו ואי אפשר לו להמלט מעבירה, עד המדריגה היו"ד, היינו מי שמרחיק עצמו מן היצה"ר ושומר עצמו מן העבירה בכל כוחו עד שאין ביכולתו לשמור א"ע יותר מזה, ואז כשנתגבר יצרו עליו ועושה מעשה אז הוא בודאי רצון הש"י, וכענין יהודא ותמר, ואיהי בת זוגו ממש וזה הענין היה גם כאן, כי זמרי היה באמת שומר עצמו מכל התאוות הרעות, ועתה עלתה בדעתו שהיא בת זוגו מאחר שאין בכוחו לסלק א"ע מזה המעשה ופנחס אמר להיפך שעדיין יש בכוחו לסלק עצמו מזה, וזה שמרמז הגמ' (סנהדרין פ"ב:) ו' נסים נעשו לפנחס, וכדאיתא שם אלו פירש זמרי והרגו לפנחס היה פטור, כי באמת היה כאן שקול הדעת, כי גם על פנחס היה מקום לבעה"ד לחלוק שמצוי בו מדה הנראה לעינים שהוא כעס כמו שבאמת טענו עליו זאת, והיה הדין בזה שודא דדיינא ופירש ר"ת [תוס'ב"ב ל"ה. ד"ה שודא] ע"ז שיכולת הדיין ליתן לקרוביו, ולכך יצא פנחס זכאי לפי שהיה קרוב למרבע"ה, וז"ש ושם איש ישראל המוכה, היינו שהקב"ה הראה לפנחס אחר מעשה שעשה עם מי היה המלחמה שלו, שאל יחשוב כי נואף גמור היה ח"ו, ופנחס מחמת שהיה בא מזרע יוסף שנתברר בסיגופים ונסיונות בענין זה ולכך הרע בעיניו מאד על מעשה זמרי, וע"ז נאמר (הושע י"א,א') כי נער ישראל ואוהבהו, וזה ממש ענין פנחס שהיה דן את זמרי לנואף בעלמא, ע"כ דן אותו קנאין פוגעין בו ונעלם ממנו עומק יסוד הדבר שהיה בזמרי, כי היא היתה בת זוגו מששת ימי בראשית, כמו שמבואר בכתבי האר"י ז"ל, עד שמרבע"ה לא הכניס א"ע בזה לדונו במיתה, ונמצא שפנחס היה במעשה הזה כנער היינו שלא היה יודע עמקות הדבר רק עפ"י עיני שכל אנושי ולא יותר, ואעפ"כ הש"י אוהבו והסכים עמו, כי לפי שכלו עשה דבר גדול בקנאתו ומסר את נפשו.
And you shouldn't think to say (chas v'shalom) that Zimri was an adulterer (chas v'shalom), for the K"BH wouldn't make a whole section of the Torah out of an adulterer. Rather, there is a hidden mystery in this matter. For there are ten levels of lust; the first level is one who adorns themselves and explicitly goes out for sinful purposes. This is a person who presents themselves to the Yetzer ha-Ra. But after this, for the remaining nine levels, at each point a person looses some of their free will, and it becomes impossible for them to resist the sin. By the 10th level, where someone has completely distanced themselves from the Yetzer ha-Ra and protected themselves from the sin with all their power, so much so that it would impossible to protect themselves anymore than they already have -- and even then, their desire overpowers them, and they commit the act, at that point they know for certain that their actions are the will of ha-Shem. This was the case with Yehudah and Tamar, for she was ordained to be his life partner. And it is the same case here, for Zimri was truly protecting himself from all evil desires, yet here arose the thought that she [Kozbi] was his life partner. And afterwards it was not within his power to refrain from this deed. Yet Pinchas spoke [as if it were] the opposite, that he [Zimri] had the strength to refrain from this.
The Gemara (Sanhedrin 82b) alludes to this: "six miracles were performed for Pinchas," as it brings there, that if Zimri were physically separated [from Kozbi at the time of Pinchas' attack], and if Zimri [defended himself and] killed Pinchas, then he [Zimri] would not be legally liable [for murder]. Because this was truly a case of reasonable doubt, yet Pinchas took upon himself the role of a chief judge, to determine the motivations as they appeared to him [alone], and he [Pinchas, viewed what was happening through the lens of] anger...Pinchas was a descendant [or the spiritual reincarnation] of Yosef, who went through a process of ethical clarification through trails and tribulations about this very matter [adultery / lust]. Therefore, Zimri's deeds seemed particularly evil to Pinchas.
Concerning this, the text says (Hoshea 11:1), "For Israel is a foolish youth, yet I love him." This is truly the case with Pinchas, who abrogated to himself the role of judge over Zimri and ruled him an adulterer, leading him to zealotry and to attack him [Zimri]. Yet the foundational depths of the matter were hidden from Pinchas, namely that she [Kozbi] had been ordained at Zimri's soul mate from the six days of Creation, as is explained in the writings of the Ari"zal. Even though Moshe Rabbeinu did not involve himself in this capital judgement, we find Pinchas acted as a foolish youth, because he did not understand the depths of the issue, and only saw what the everyday person would see and no more. Yet even so, ha-Shem loved him, and gave him approval, because he acted in accordance with his own understanding in what [he thought] was a great deed of zealotry, so [ha-Shem] guided him spiritually. Yet Pinchas spoke [as if it were] the opposite, that he [Zimri] had the strength to refrain from this. The Gemara (Sanhedrin 82b) alludes to this: "six miracles were performed for Pinchas," as it brings there, that if Zimri were physically separated [from Kozbi at the time of Pinchas' attack], and if Zimri [defended himself and] killed Pinchas, then he [Zimri] would not be legally liable [for murder]. Because this was truly a case of reasonable doubt, yet Pinchas took upon himself the role of a chief judge, to determine the motivations as they appeared to him [alone], and he [Pinchas, viewed what was happening through the lens of] anger....Pinchas was a descendant [or the spiritual reincarnation] of Yosef, who went through a process of ethical clarification through trails and tribulations about this very matter [adultery / lust]. Therefore, Zimri's deeds seemed particularly evil to Pinchas. Concerning this, the text says (Hoshea 11:1), "For Israel is a foolish youth, yet I love him." This is truly the case with Pinchas, who abrogated to himself the role of judge over Zimri and ruled him an adulterer, leading him to zealotry and to attack him [Zimri]. Yet the foundational depths of the matter were hidden from Pinchas, namely that she [Kozbi] had been ordained at Zimri's soul mate from the six days of Creation, as is explained in the writings of the Ari"zal. Even though Moshe Rabbeinu did not involve himself in this capital judgement, we find Pinchas acted as a foolish youth, because he did not understand the depths of the issue, and only saw what the everyday person would see and no more. Yet even so, ha-Shem loved him, and gave him approval, because he acted in accordance with his own understanding in what [he thought] was a great deed of zealotry, so [ha-Shem] guided him spiritually.