As mentioned here, you have to use dynamic SQL.
First approach is where you specify table name yourself:
declare @tablename varchar(30), @SQL varchar(30)
set @tablename = 'Table1' --here you specify the name
set @SQL = concat('SELECT COUNT(*) FROM ', @tablename) --here you build the query
EXEC(@SQL)
Second approach lets you use table with names of tables:
declare @SQL varchar(8000)
set @SQL = ''
declare @TableNames table(name varchar(30))
insert into @TableNames values ('Table1'), ('Table2'), ('Table3')
--here you build the query
select @SQL = @SQL + ' SELECT ''' + name + ''' AS [TableName], COUNT(*) AS [Count] FROM ' + name + ' UNION ALL' from @TableNames
-- get rid of last "UNION ALL"
set @SQL = LEFT(@SQL, LEN(@SQL) - 10)
--execute the query
EXEC(@SQL)
The result of it will be:
TableName Count
Table1 3
Table2 6
Table3 4