The main name is the attribute of Justice while the four letter Name is for the attribute of Mercy. The malach appeared to save his life and give him a chance to act properly.
וַיִּחַר אַף אֱלֹהִים כִּי הוֹלֵךְ הוּא וַיִּתְיַצֵּב מַלְאַךְ יְהֹוָה
בַּדֶּרֶךְ לְשָׂטָן לוֹ וְהוּא רֹכֵב עַל אֲתֹנוֹ וּשְׁנֵי נְעָרָיו
As we see Hashem became angry with Bil'am because of his insistence on not doing what Hashem wanted and deserving the death penalty. As a result, the malach appearing to stop him was an expression of mercy.
As an example
Justice and Mercy
“And Elokim (G-d of Justice) spoke unto Moshe saying: I am YHVH (G-d
of Mercy)” (6:2).
Thus, when Hashem forbade Bil'am from cursing the Bnei Yisrael, He did not do so because He was exhibiting mercy, but because He was showing that they did not deserve to be cursed based on the attribute of Justice. When He sent the malach to give Bil'am a final chance, He was exhibiting the trait of Mercy by allowing him a chance to act properly of his own free will. Every reference from 22:22 to 22:35 uses the Name of Hashe (Mercy) for that reason.
As Rav Hirsch translates Balak 23:4
וַיִּקָּר אֱלֹהִים אֶל בִּלְעָם וַיֹּאמֶר אֵלָיו אֶת שִׁבְעַת
הַמִּזְבְּחֹת עָרַכְתִּי וָאַעַל פָּר וָאַיִל בַּמִּזְבֵּחַ:
And Elokim let it happen to Balaam ...
Once Bil'am rejected the trait of Mercy, he was again treated with the attribute of Justice
as he explains on verse 5.
וַיָּשֶׂם יְהֹוָה דָּבָר בְּפִי בִלְעָם וַיֹּאמֶר שׁוּב אֶל בָּלָק
G0d did not grant a vision to which he would then have to give an
explanation and meaning in words of what he took the vision to mean,
but put in his mouth the actual words he was to say.