Why does Poland give away so much monetary aid to Ukraine even though it took two thirds of the Ukrainian refugees?
Do they have a budget surplus?
Is the Polish popular opinion pressing the government?
Is there any other reason?
Poland is on the front lines by bordering Belarus which made itself available as a staging area for a Russian invasion of Ukraine. This is an existential threat to Poland.
Ukrainian refugees in Poland (about 2 million refugees in a country with a pre-war population of about 38 million people according to the question, although this is a moving target), if anything, provide the Poles with a daily reminder of that the threat of Russian military action that once would have been unthinkable in light of treaties in place, etc. is very real.
But ultimately, there is very little direct connection between receiving refugees from Ukraine and providing economic support to Ukraine. One doesn't impact the ability to do the other, or vice versa. Accepting a refugee isn't necessary particularly draining on the government budget - not free, but not necessarily particularly expensive either and possibly a net gain for Poland economically in the medium to long run. The question's implied premise that refugees are such a drain on Poland's government funds that this seriously squeezes its ability to afford to provide financial aid to Ukraine is flawed.
In the short run, Poland is spending 1% of its GDP ($5.3 billion USD equivalent) on aid to Ukrainian refugees, which is significantly smaller than its defense budget (2.2% of GDP in 2022, 3% in 2023 and with a longer term target of 5%-6% of GDP in light of the heightened threat from Russia which is a sudden unexpected crisis.) As the question notes, Poland's foreign aid to Ukraine is 0.49% of Poland's GDP and if the war in Ukraine isn't too long, that expenditure may not last too long.
In 2022, Poland had an annual budget deficit of 1.9% of GDP, but deficit spending is normal in a national emergency or crisis. Essentially, its refugee spending and foreign aid and ramped up defense spending are being financed with government debt. Poland's annual deficits as a percentage of GDP each year for the last 25 years (according to Bloomberg) is shown below:
But, Poland's government finance situation is actually pretty fiscally sound by historic, post-Cold War standards, at the moment.
Every Russian tank and artillery battery destroyed, and every Russian general killed, by a Ukrainian soldier with resources that Polish funds make possible is one that a Polish soldier doesn't have to shed blood to destroy or kill if Russia attacks it. This is because Russia doesn't have a sufficient industrial base to replace its military losses promptly and countries like China that are willing to buy its oil aren't willing to sell Russia more advanced weapons.
And, the more Russia's military capacity in Eastern Europe is depleted in the Ukraine War, the less likely it is that Russia will even attempt to take military action against Poland at all (avoiding the lost lives and damage to property that comes with having a war fought on your own territory). While Russia hasn't thrown all of its military capacity in Eastern Europe into the Ukraine War, it has devoted a very large share of that military capacity to the Ukraine War, so losses in Ukraine undermine its ability to attack Poland.
It is a smart national defense investment to use your money to allow someone else to fight your most likely military opponent on their territory with their soldiers before you must fight that opponent on your territory with your soldiers.
It doesn't even really matter what the aid is ear-marked for, since money is fungible, and non-military foreign aid frees up Ukraine's funds for military spending, and lots of countries are willing to sell advanced arms to Ukraine at the moment.
So one of the main reasons would be to prevent a possible dissolution of Ukraine in this case Poland would have to deal with much more refugees from Ukraine as now. If you take a look at other countries giving so much and even more aid to Ukraine you see the Baltic states. The Baltic state and Poland are solidaric with Ukraine as fighting the common enemy. All these countries seeing themself next to be attacked by Russia should Ukraine fall.
Poland said before 2022 that regional powers should handle refugees.
Poland is providing quite a lot, per capita. During the 2015 refugee crisis, they refused to pitch in, citing their preparations for an Ukrainian contingency. Many who criticized them back then have since apologized. However:
In short, the Poles historically have always been hating the Russians. This goes back as far as the war of 1605–1618, when union of Poland, Lithuania, and the Zaporozhian Cossacks first captured but then lost the city of Moscow. Warsaw also allied with the cruel, antisemitic Petliura in 1920 in his fight against the Bolshevik régime. In the 21st century, Warsaw wishes to take more influence than during the Soviet era. Naturally, Poland is interested in splitting the Russians and the Ukrainians and in fighting with the hands of the Ukrainians against the Russians. This is the best that can happen to the Poles regardless of the refugees. The enemy of your enemy is your friend, and finally Poland can spend a lot towards this goal rather than trying to solve domestic problems.
Thus, Poland is trying to increase its influence in Ukraine. So, in response to Polish assistance, the Ukrainian parliament (Rada) adopted a law on the special status of Polish citizens in Ukraine.
The law establishes that citizens of the Republic of Poland, their spouses and children who entered Ukraine from the territory of the Republic of Poland for temporary residence, as well as their children born in Ukraine have the right to legally stay in the territory of Ukraine for 18 months from the day the law comes into force. The period of stay can be extended provided there are relevant supporting documents (enrolment in a higher education institution, move to the next academic year, conclusion of an employment contract, etc.). It is also stipulated that citizens of the Republic of Poland during their stay in Ukraine are entitled to:
employment without a work permit for foreigners and stateless persons;
state registration as taxpayers and obtaining a registration number of a taxpayer’s record card in accordance with the legislation of Ukraine;
social security, including the right to assistance in case of full, partial or temporary disability, loss of provider, unemployment due to circumstances beyond their control in accordance with the legislation and international treaties of Ukraine;
carrying out economic activity under the same conditions as citizens of Ukraine;
receiving education on the same conditions as citizens of Ukraine, including at the expense of the state or local budget
social payments (state assistance) according to the legislation of Ukraine;
receipt of assistance from the Fund for social protection of persons with disabilities on the same terms as the citizens of Ukraine;
free medical assistance in public and communal health institutions on the same conditions as those for citizens of Ukraine.