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 visavice 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.