Good question. The truth is that during times of war, a soldier is allowed to eat non-kosher food if kosher food is not available. The way the Rambam sounds, allowing foreign women is a similar concession, but the Torah put a *restriction* on it that the soldier must marry the woman, and not just leave her. [See here.][1]

> חֲלוּצֵי צָבָא כְּשֶׁיִּכָּנְסוּ בִּגְבוּל הָעַכּוּ''ם וְיִכְבְּשׁוּם וְיִשְׁבּוּ מֵהֶן. מֻתָּר לָהֶן לֶאֱכל נְבֵלוֹת וּטְרֵפוֹת וּבְשַׂר חֲזִיר וְכַיּוֹצֵא בּוֹ אִם יִרְעַב וְלֹא מָצָא מַה יֹּאכַל אֶלָּא מַאֲכָלוֹת אֵלּוּ הָאֲסוּרִים. וְכֵן שׁוֹתֶה יֵין נֶסֶךְ. מִפִּי הַשְּׁמוּעָה לָמְדוּ וּבָתִּים מְלֵאִים כָּל טוּב עָרְפֵּי חֲזִירִים וְכַיּוֹצֵא בָּהֶן:  
Those courageous soldiers (or front-line troops) who cross the borders into non-Jewish lands, capture them and take prisoners are permitted to eat non-Kosher meats, swine and so on, if they are hungry and could not find anything to eat except for these forbidden foods. They may also drink wine offered in the service of idolatry. By Tradition we have learned that “houses filled with all good” (Deut. 6:11) means beheaded boar, and the like.     
2
וְכֵן בּוֹעֵל אִשָּׁה בְּגֵיוּתָהּ אִם תְּקָפוֹ יִצְרוֹ. אֲבָל לֹא יִבְעָלֶנָּה וְיֵלֵךְ לוֹ. אֶלָּא מַכְנִיסָהּ לְתוֹךְ בֵּיתוֹ. שֶׁנֶּאֱמַר (דברים כא, יא) "וְרָאִיתָ בַּשִּׁבְיָה אֵשֶׁת יְפַת תֹּאַר". וְאָסוּר לִבְעל אוֹתָהּ בִּיאָה שְׁנִיָּה עַד שֶׁיִּשָּׂאֶנָּה:            
 Similarly, one may have sexual intercourse with a non-Jewish woman if his lust gets the better of him. But, he must not just have intercourse with her and go. Rather, he must take her home, as it says, “and when you see a Woman of Beauty among the captives” (Deut. 21:11). He may not have intercourse with her a second time until he marries her.

The reason is generally understood to be that in times of war, when soldiers are focused on basic matters of survival, it is not possible to expect them to strictly adhere to the laws. Therefore, they can eat non-kosher food if necessary, and if they have a strong desire for a woman, they can fulfill it. So the permission granted for a craving is limited to the background situation of a war, but would not apply otherwise. 


  [1]: https://www.sefaria.org/Mishneh_Torah%2C_Kings_and_Wars.8.1?ven=Laws_of_Kings_and_Wars._trans._Reuven_Brauner,_2012&lang=bi&with=all&lang2=en