Script that creates a mailbox (folder) based on the email subject
I was able to get help from the official Apple Discussions forum, thanks to Barney15-E. Here's the reply:

In order to trigger it with a shortcut, you would need to create an Automator Quick Action.
Within that Quick Action you can run this script:

tell application "Mail"
    set selectedMessages to selection
    set mailboxName to subject of item 1 of selectedMessages
    set messageAccount to account of (mailbox of item 1 of selectedMessages)
    set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
    repeat with eachMessage in selectedMessages
        set mailbox of eachMessage to newMailbox
    end repeat
end tell


To create the Quick Action, open Automator.
Select Quick Action from the document type selection.
Set the Quick Action to accept No Input in Mail.
Set the Image and Color to anything you want.
Drag a "Run AppleScript" action into the workflow area.
Replace everything between the on run/end run statements with the script above.
Save the Quick Action with a suitable name.

To give the Quick Action a shortcut, open Keyboard System Preferences.
Click on Shortcuts tab.
Select Services from the list.
Find your Quick Action (probably under General).
Add the desired shortcut.

If you want to file each selected message into its own folder, use this script instead:

tell application "Mail"
    set selectedMessages to selection
    repeat with eachMessage in selectedMessages
        set mailboxName to subject of eachMessage
        set messageAccount to account of (mailbox of eachMessage)
        set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
        set mailbox of eachMessage to newMailbox
    end repeat
end tell


If you want a version that checks for at least one message selected, use this:

tell application "Mail"
    set selectedMessages to selection
    if selectedMessages is not {} then
        set mailboxName to subject of item 1 of selectedMessages
        set messageAccount to account of (mailbox of item 1 of selectedMessages)
        set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
        repeat with eachMessage in selectedMessages
            set mailbox of eachMessage to newMailbox
        end repeat
    else 
        display dialog "You did not select a message."
    end if
end tell