You can do the following steps to achieve this:
1) Navigate to https://aad.portal.azure.com/#view/Microsoft_AAD_IAM/GroupsManagementMenuBlade/\~/AllGroups
2) Choose New Group
3) Choose Group Type > Microsoft 365
4) Choose Membership Type > Dynamic User
5) Add Dynamic Query
Edit the rule syntax and use the example (this will remove them from the group if their account is not enabled):
(user.accountEnabled -eq true) and ((user.mail -eq "******@mail.com") or (user.mail -eq "******@mail.com") or (user.mail -eq "******@mail.com"))
6) After you create the group go to Licenses under Manage:
7) Go to the Assign button and assign the licenses you want to these users.
If this is helpful please accept answer.